package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.client.OssClient;
import com.czz.manage.entity.Ad;
import com.czz.manage.mapper.AdMapper;
import com.czz.manage.query.AdQuery;
import com.czz.manage.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @ClassName: AdServiceImpl
 * @Author Czz
 * @Date 2020/2/5
 * @Time 17:30
 **/
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    @Autowired
    AdMapper adMapper;

    @Autowired
    OssClient ossClient;

    @Override
    public void getAdPageList(Page<Ad> pageParam, AdQuery adQuery) {
        QueryWrapper<Ad> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("ad_status");
        queryWrapper.orderByDesc("ad_sort");
        queryWrapper.orderByDesc("gmt_create");

        if (ObjectUtils.isEmpty(adQuery)){
            adMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String name = adQuery.getName();
        String begin = adQuery.getBegin();
        String end = adQuery.getEnd();
        Integer status = adQuery.getStatus();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("ad_name",name);
        }

        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("ad_status",status);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        adMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void updateAdStatusById(String id, Integer status) {
        Ad ad = new Ad();
        ad.setId(id);
        ad.setStatus(status);
        adMapper.updateById(ad);
    }

    @Override
    @Transactional
    public Boolean deleteAdById(String id) {
        //轮播图至少存在一张，先判断数量
        Integer count = adMapper.selectCount(null);

        //只有一张
        if (count <= 1){
            return false;
        }

        //存在多张
        String OSS_URL = "http://czz-education.oss-cn-shenzhen.aliyuncs.com/";
        Ad ad = new Ad();
        ad = adMapper.selectById(id);

        //删除oss中图片
        String pic = ad.getPic().replace(OSS_URL,"").replaceAll("/","&&");
        ossClient.deleteOssFile(pic).getMessage();

        //删除数据库中数据
        adMapper.deleteById(id);
        return true;
    }

    @Override
    public void updateAdById(Ad ad) {
        String OSS_URL = "http://czz-education.oss-cn-shenzhen.aliyuncs.com/";

        //判断轮播图地址是否修改
        Ad temp = adMapper.selectById(ad.getId());

        //删除oss中轮播图
        if (!temp.getPic().equals(ad.getPic())){
            String pic = temp.getPic().replace(OSS_URL,"").replaceAll("/","&&");
            ossClient.deleteOssFile(pic);
        }

        //修改数据库内容
        adMapper.updateById(ad);
    }
}
